import com.alibaba.fastjson.JSON;
import com.aliyun.odps.udf.UDF;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.HashMap;
import java.util.Map;

public class SpliderWalgreensGoodsInfo extends UDF {
  /*  public static void main(String[] args) {
        HttpClientAPI client = new HttpClientAPI();
        try {
            HttpResult httpResult = client.doGet("https://www.walgreens.com/store/c/sheamoisture-kids-detangler-coconut/hibiscus/ID=prod6229097-product");
            System.out.println("request return");
            new SpliderWalgreensGoodsInfo().evaluate(httpResult.getBody());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/

    public String evaluate(String html){
        if(StringUtils.isNotBlank(html)){
            Map<String,String> resultMap = new HashMap<>();
            Document document = Jsoup.parse(html);
            //获取title
            Element titleEle = document.getElementById("productTitle");
            if (null != titleEle){
                String title = titleEle.text();
                resultMap.put("title",title);
            }
            //品牌
            Elements brandEles = document.getElementsByClass("brand-title font__eighteen");
            if (null != brandEles){
                String brand = brandEles.text();
                resultMap.put("brand",brand);
            }
            //价格
            String price = null;//regular-price-wag-hn-lt-bold
            Elements elementsByClass = document.getElementsByClass("price__contain regular-price wag-cleareance-space");
            if(null != elementsByClass && elementsByClass.size() != 0){
                Elements priceEles = elementsByClass.get(0)
                        .getElementsByClass("product__price");
                if(null != priceEles){
                    //因为价格之间有小数点，页面中没有加上
                    Elements spanEles = priceEles.get(0).getElementsByTag("span");
                    if(null != spanEles && spanEles.size() > 0){
                        for (Element span :spanEles){
                            if(StringUtils.isNumeric(span.text())){
                                price = span.text();
                                Elements supEles = priceEles.get(0).getElementsByTag("sup");
                                for(Element sup :supEles){
                                    if(StringUtils.isNumeric(sup.text())){

                                        price = price+"."+sup.text();
                                    }else{
                                        price = sup.text()+price;
                                    }
                                }
                            }
                        }
                    }
                }
            }else {
                Element elementById = document.getElementById("regular-price-wag-hn-lt-bold");
                if(null != elementById){
                    price = elementById.text();
                }
            }
            resultMap.put("price",price);

            //分类
            Element categoryEle = document.getElementById("breadcrumbs-desktop");
            if(null != categoryEle)   {
                Elements liEles = categoryEle.getElementsByTag("li");
                if(null != liEles && liEles.size() > 0){
                    String category = liEles.get(liEles.size() - 1).text();
                    resultMap.put("category",category);
                }
            }

            //容量
            Element volumeEle = document.getElementById("productSizeCount");
            if (null != volumeEle){
                String volume = volumeEle.text();
                Element prodPacksAvailable = document.getElementById("prodPacksAvailable");
                if(null != prodPacksAvailable){
                    volume = volume+" "+prodPacksAvailable.text();
                }
                resultMap.put("volume",volume);
            }
            //单价
            Element unitPriceEle = document.getElementById("unit-price");
            if (null != unitPriceEle){
                String unitPrice = unitPriceEle.text();
                resultMap.put("unitPrice",unitPrice);
            }else{
                Elements elementsByClass1 = document.getElementsByClass("wag-unit-price-position wag-vpd-font-14");
                if(null != elementsByClass1){
                    resultMap.put("unitPrice",elementsByClass1.text());
                }
            }
            //星级
            Element levelEle = document.getElementById("prodBV");
            if (null != levelEle){
                String level = levelEle.text();
                resultMap.put("level",level.replaceAll("\\(.+\\)", ""));
            }
            //图片
            Element imgHrefEle = document.getElementById("productImg");
            if (null != imgHrefEle){
                String imgHref = imgHrefEle.attr("src");
                resultMap.put("imgHref",imgHref);
            }
            //描述
            Element prodDescEle = document.getElementById("prodDesc");
            if (null != prodDescEle ){
                String prodDesc = prodDescEle.text();
                if(prodDesc.startsWith("Description")){
                    prodDesc = prodDesc.replace("Description","");
                }
                resultMap.put("prodDesc",prodDesc.replace("Show More","").trim());
            }
            //成分
            Element ingredientsEle = document.getElementById("Ingredients");
            if (null != ingredientsEle){
                String ingredients = ingredientsEle.getElementsByClass("inner").text();
                resultMap.put("ingredients",ingredients);
            }

            Element info = document.getElementById("Shipping Specifications");
            if(null != info){
                //规格 产品英寸 Product in inches (LxWxH):
                String size = info.getElementsByClass("universal-product-inches").get(0).text();
                resultMap.put("size",size);
                //编号
                String itemCode = info.getElementsByClass("universal-product-code").get(0).text();
                resultMap.put("productCode",itemCode.replace("Item Code: ",""));
                //UPC
                String upc = info.getElementsByClass("universal-Item-code").get(0).text();
                resultMap.put("upc",upc.replace("UPC: ",""));
            }

            //警告
            Element warningsEle = document.getElementById("Warnings");
            if(null != warningsEle){
                String warnings = warningsEle.text();
                resultMap.put("warnings",warnings);
            }

            //标签
            Elements tagEles = document.getElementsByClass("block wellness-pins");
            if(null != tagEles && tagEles.size() >0){
                StringBuffer sb = new StringBuffer();
                Elements li = tagEles.get(0).getElementsByTag("li");
                for (Element e : li ){
                    Elements elementsByClass1 = e.getElementsByClass("tool-tip__title");
                    System.out.println(elementsByClass1.text());
                    sb = sb.append(elementsByClass1.text()).append("@!@");
                }
                resultMap.put("tag",sb.substring(0,sb.lastIndexOf("@!@")));
            }

            System.out.println(JSON.toJSONString(resultMap));
            return JSON.toJSONString(resultMap);
        }
       return null;
    }
}
